<template>
  <div class="right-container">
    <div class="container-control">
      <div class="right-control">
        <div @click="changeTag(1)" :class="{ active: tag == 1 }" class="rt-btn">
          汽&nbsp;&nbsp;运
        </div>
        <div @click="changeTag(2)" :class="{ active: tag == 2 }" class="rt-btn">
          火&nbsp;&nbsp;车
        </div>
      </div>
    </div>
    <div class="container-body">
      <div class="cont-1">
        <PortPlanInfo :tag="tag" />
      </div>
      <div class="cont-2">
        <PortSurplusInfo :tag="tag" />
      </div>
      <div class="cont-3">
        <PortTransportInfo ref="portTransportInfo" :tag="tag" />
      </div>
      <div class="cont-4">
        <PortSuccessInfo :tag="tag" />
      </div>
    </div>
  </div>
</template>

<script>
import PortPlanInfo from "./PortPlanInfo.vue";
import PortSurplusInfo from "./PortSurplusInfo.vue";
import PortTransportInfo from "./PortTransportInfo.vue";
import PortSuccessInfo from "./PortSuccessInfo.vue";
import { getStation } from "@/api/networShow/dataV";
import { mapState } from "vuex";
export default {
  components: {
    PortPlanInfo,
    PortSurplusInfo,
    PortTransportInfo,
    PortSuccessInfo
  },
  data() {
    return {
      tag: 1,
      trainStationJson: { type: "FeatureCollection", features: [] }
    };
  },
  computed: {
    ...mapState({
      centerMap: state => state.dataV.centerMap,
      centerMapOnload: state => state.dataV.centerMapOnload
    }),
    pointTrainStation() {
      // 用于检测车站数据及map加载情况
      return {
        centerMapOnload: this.centerMapOnload,
        trainStationJson: this.trainStationJson
      };
    }
  },
  watch: {
    // 车站数据及map加载完成
    pointTrainStation: {
      deep: true,
      handler(newVal) {
        if (newVal.centerMapOnload && newVal.trainStationJson.features.length) {
          this.centerMap.addTrainStation(this.trainStationJson);
        }
      }
    }
  },
  methods: {
    changeTag(tag) {
      this.tag = tag;
      this.$store.commit("dataV/UPDATE_RIGHTTAG", tag);
      // 移除弹框
      this.centerMap.resetMap();
      // 移除 精品线路
      this.centerMap.clearLine();
      // 移除 精品线路点
      this.centerMap.clearStartEndPoint();
      //  添加地图上港口数据
      this.centerMap.addDefaultPort();
      if (tag == 2) {
        this.getStation();
      } else if (tag == 1) {
        // 移除火车站
        this.centerMap.clearTrainStation();
        this.$refs.portTransportInfo.getListData();
      }
    },
    getStation() {
      return getStation().then(res => {
        const { data } = res;
        if (data.code == 200) {
          const trainStationJson = {
            type: "FeatureCollection",
            features: []
          };
          data.data.forEach(item => {
            const point = {
              type: "Feature",
              properties: {
                name: item.stationName,
                coordinates: [item.lon, item.lat]
              },
              geometry: {
                type: "Point",
                coordinates: [item.lon, item.lat]
              }
            };
            trainStationJson.features.push(point);
          });
          this.trainStationJson = trainStationJson;
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.right-container {
  width: 615px;
  height: 100%;
  box-sizing: border-box;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .container-control {
    height: 23px;
    display: flex;
    justify-content: flex-end;
    .right-control {
      font-size: 10px;
      font-family: SourceHanSansCN-Medium, SourceHanSansCN;
      font-weight: 500;
      color: #14275b;
      &:last-child {
        margin-right: 7px;
      }
      .rt-btn {
        cursor: pointer;
        display: inline-block;
        position: relative;
        padding: 0 10px;
        margin-right: 1px;
        box-sizing: border-box;
        &::before {
          content: "";
          position: absolute;
          top: 0;
          right: 0;
          bottom: 0;
          left: 0;
          background: linear-gradient(270deg, #29457a 0%, #537bb2 100%);
          z-index: -1;
          transform: skew(-30deg);
        }
        &.active {
          color: #87bcff;
          font-weight: bold;
        }
        &:hover {
          opacity: 0.9;
        }
      }
    }
  }
  .container-body {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .cont-1 {
      height: 293px;
    }
    .cont-2 {
      height: 241px;
    }
    .cont-3 {
      height: 255px;
    }
    .cont-4 {
      height: 185px;
    }
  }
}
</style>
